/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.member;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.server.modules.api.entity.BankCardInfoModel;
import com.server.modules.api.service.impl.BankCardPingxxServiceImpl;
import com.server.modules.api.vo.BankCardInfoVo;
import com.server.modules.api.vo.BaseQueryVo;
import com.server.modules.sys.entity.User;
import com.server.modules.sys.entity.bank.BankCardBin;
import com.server.modules.sys.service.bank.BankCardBinService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.web.BaseController;
import com.server.common.utils.StringUtils;
import com.server.modules.sys.entity.member.MemberBankcard;
import com.server.modules.sys.service.member.MemberBankcardService;

import java.util.Date;

/**
 * 绑定银行卡管理Controller
 * @author 王磊
 * @version 2016-11-15
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/member/memberBankcard")
public class MemberBankcardController extends BaseController {



	@Resource
	protected BankCardPingxxServiceImpl bankCardPingxxService;

	@Resource
	private MemberBankcardService memberBankcardService;

	@Resource
	BankCardBinService bankCardBinService;
	
	@ModelAttribute
	public MemberBankcard get(@RequestParam(required=false) String id) {
		MemberBankcard entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = memberBankcardService.get(id);
		}
		if (entity == null){
			entity = new MemberBankcard();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:member:memberBankcard:view")
	@RequestMapping(value = {"list", ""})
	public String list(MemberBankcard memberBankcard, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MemberBankcard> page = memberBankcardService.findPage(new Page<MemberBankcard>(request, response), memberBankcard); 
		model.addAttribute("page", page);
		model.addAttribute("memberBankcard", memberBankcard);
		return "modules/sys/member/memberBankcardList";
	}

	@RequiresPermissions("sys:member:memberBankcard:view")
	@RequestMapping(value = "form")
	public String form(MemberBankcard memberBankcard, Model model) {
		model.addAttribute("memberBankcard", memberBankcard);
		return "modules/sys/member/memberBankcardForm";
	}

	@RequiresPermissions("sys:member:memberBankcard:edit")
	@RequestMapping(value = "save")
	public String save(MemberBankcard memberBankcard, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, memberBankcard)){
			return form(memberBankcard, model);
		}
		memberBankcardService.save(memberBankcard);
		addMessage(redirectAttributes, "保存银行卡绑定成功");
		return "redirect:"+Global.getAdminPath()+"/sys/member/memberBankcard/?repage";
	}
	
	@RequiresPermissions("sys:member:memberBankcard:edit")
	@RequestMapping(value = "delete")
	public String delete(MemberBankcard memberBankcard, RedirectAttributes redirectAttributes) {
		memberBankcardService.delete(memberBankcard);
		addMessage(redirectAttributes, "删除银行卡绑定成功");
		return "redirect:"+Global.getAdminPath()+"/sys/member/memberBankcard/?repage";
	}
	//银行卡绑定
	@ResponseBody
	@RequestMapping(value = "/bindBankCard")
	public boolean bindBankCard(HttpServletRequest request,MemberBankcard vo){
		try{
			MemberBankcard memberBankcard = bankCardPingxxService.getBankcardByMemberId(vo.getMemberId());
			if (memberBankcard!=null){
				return  false;
			}else {
				memberBankcard = bankCardPingxxService.getBankcardByCardNum(vo.getCardNumber());
				if (memberBankcard!=null && !vo.getMemberId().equals(memberBankcard.getMemberId())){
					return false;
				}
			}
			BaseQueryVo baseQueryVo = new BaseQueryVo();
			BankCardInfoVo bankCardInfoVo = new BankCardInfoVo();
			bankCardInfoVo.setType("bank_card");
			BankCardInfoModel bankCardInfoModel = new BankCardInfoModel();
			bankCardInfoModel.setCard_number(vo.getCardNumber());
			bankCardInfoModel.setId_name(vo.getIdName());
			bankCardInfoModel.setId_number(vo.getIdNumber());
			bankCardInfoModel.setPhone_number(vo.getPhoneNumber());
			bankCardInfoVo.setData(bankCardInfoModel);
			baseQueryVo=   bankCardPingxxService.identifyBankCard(bankCardInfoVo);
			if (baseQueryVo.getRetCode()==0){
				memberBankcard = new MemberBankcard();
				memberBankcard.setMemberId(vo.getMemberId());
				memberBankcard.setCardNumber(bankCardInfoModel.getCard_number());
				memberBankcard.setIdName(bankCardInfoModel.getId_name());
				memberBankcard.setPhoneNumber(bankCardInfoModel.getPhone_number());
				memberBankcard.setIdNumber(bankCardInfoModel.getId_number());
				User user = new User();
				user.setId("1");
				memberBankcard.setCreateDate(new Date());
				memberBankcard.setCreateBy(user);
				memberBankcard.setUpdateBy(user);
				memberBankcard.setUpdateDate(new Date());
				memberBankcard.setDelFlag("0");
				BankCardBin bankCardBin = bankCardPingxxService.getBankCardBinByCardNum(bankCardInfoModel.getCard_number());
				if (bankCardBin!=null){
					memberBankcard.setBankName(bankCardBin.getBankName());
					memberBankcard.setCardName(bankCardBin.getCardName());
					memberBankcard.setCardType(bankCardBin.getCardType());
				}else {
					bankCardBin = new BankCardBin();
					bankCardBin.setDemo(bankCardInfoModel.getCard_number());
					bankCardBin.setDelFlag("2");//未查询到相应的卡bin
					bankCardBin.setCardDigits(""+bankCardInfoModel.getCard_number().trim().length());
					bankCardBin.setCreateBy(user);
					bankCardBin.setCreateDate(new Date());
					bankCardBin.setUpdateBy(user);
					bankCardBin.setUpdateDate(new Date());
					bankCardBinService.save(bankCardBin);
				}
				memberBankcardService.save(memberBankcard);
				return true;
			}else{
				return false;
			}

		}catch (Exception e){
			e.printStackTrace();
			return  false;
		}
	}

}